#include <stdlib.h>
#include <glut.h>

GLfloat     rquad = 0;               // Angle For The Quad
   float LightDir1[3] = {0.0f, -0.8f, -0.6f};

int cube;


void init(void) 
{
	
	GLfloat ambient[] = { 1.0, 0.0, 0.0, 1.0 };
	GLfloat diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
	GLfloat specular[] = { 1.0, 0.0, 0.0, 1.0 };
	GLfloat position[] = { 5.0, -3.0, 0.0, 1.0 };

	GLfloat ambient2[] = { 1.0, 1.0, 0.0, 1.0 };
	GLfloat diffuse2[] = { 0.5, 0.0, 0.0, 1.0 };
	GLfloat specular2[] = { 0.5, 0.0, 0.0, 1.0 };
	GLfloat position2[] = { 0.0, 1.0, -3.5, 0.0 };

	GLfloat ambient1[] = { 0.0, 0.0, 1.0, 1.0 };
	GLfloat diffuse1[] = { 0.0, 0.0, 1.0, 1.0 };
	GLfloat specular1[] = { 0.0, 0.0, 1.0, 1.0 };
	GLfloat position1[] = { 0.0, 2.0, 1.0, 0.0 };
	
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	glLightfv(GL_LIGHT0, GL_POSITION, position);

	glLightfv(GL_LIGHT2, GL_AMBIENT, ambient2);
	glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse2);
	glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
	glLightfv(GL_LIGHT2, GL_POSITION, position2);

	glLighti(GL_LIGHT1, GL_SPOT_CUTOFF, 30);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
	glLightfv(GL_LIGHT1, GL_POSITION, position1);
	glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, LightDir1);

	

   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_SMOOTH);
   glEnable(GL_DEPTH_TEST);

   

   glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT1);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT2);
	
	GL_LIGHT_MODEL_AMBIENT;
}

void BeschornerPhilipDisplay(void)
{
   glClear(GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glEnable(GL_DEPTH_TEST);
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity ();             /* clear the matrix */

   // viewing transformation
   gluLookAt (1.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   // Look from 1.0, 2.0, 5.0 to 0.0, 0.0, 0.0 where up is 0.0, 1.0, 0.0
   glRotatef(rquad,0.0,1.0,0.0);             // Rotate The Triangle On The Y axis
   glPushMatrix();
   
     

	glColor3f(1.0f,1.0f,1.0f);  // Set The Color To a blue shade
	glLoadIdentity();
	glPopMatrix();
   glPushMatrix();
   glTranslatef(0.0f, -1.0f, 0.0f); // um 1 nach unten verschoben, dass Äste nicht 2 mal verschoben werden müssen
   glScalef (1.0, 2.0, 1.0); // Skalierung
   glutSolidCube(1.0);
   
   
	//-----------------------------------------------------------------------------------------------------------------------------------
   


   glLoadIdentity ();  
   glColor3f(1.0f,1.0f,1.0f);  //weiß
   glPopMatrix();
   glPushMatrix();
   glRotatef(60.0, 0.0f, 0.0f, 1.0f); //60 grad rotation
   glTranslatef(0.0f, 1.0f, 0.0f);
   glScalef (1.0, 2.0, 1.0);
   glutSolidCube(1.0);
	//-----------------------------------------------------------------------------------------------------------------------------------
   glLoadIdentity();
   glColor3f(1.0f,1.0f,1.0f); //weiß
   glPopMatrix();
   glPushMatrix();
   glRotatef(120.0f, 0.0f, 1.0f, 0.0f); //120 grad rotation
   glRotatef(60.0f,0.0f,0.0f,1.0f); //60 grad rotation
   glTranslatef(0.0f, 1.0f, 0.0f);
   glScalef(1.0f,2.0f,1.0f);
   glutSolidCube(1.0);
	//-----------------------------------------------------------------------------------------------------------------------------------
   glLoadIdentity(); //dritter ast
   glColor3f(1.0f,1.0f,1.0f); //weiß
   glPopMatrix();
   glRotatef(240.0f, 0.0f, 1.0f, 0.0f); //240 grad rotation
   glRotatef(60.0f,0.0f,0.0f,1.0f); //60 grad rotation
   glTranslatef(0.0f, 1.0f, 0.0f);
   glScalef(1.0f,2.0f,1.0f);
   glutSolidCube(1.0);
   
	//-----------------------------------------------------------------------------------------------------------------------------------
   /*
   glLoadIdentity(); //sphere
   glColor3f(0.0f, 1.0f, 0.0f); //grün
   glPopMatrix();
   glTranslatef(0.0f, 4.0f, 3.0f);
   glScalef(1.0f,2.0f,1.0f);
   glutSolidSphere(0.5,2,2);
   */

   //-----------------------------------------------------------------------------------------------------------------------------------

   


   glutSwapBuffers();

}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   glMatrixMode (GL_MODELVIEW);
}



void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

void Idle( void )
{
	rquad += 0.8; //drehung um faktor
	if (rquad> 360)
		rquad = 0;
	glutPostRedisplay();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(BeschornerPhilipDisplay); 
   glutReshapeFunc(reshape);
   glutIdleFunc(Idle);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   
   return 0;
}